<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;

class RolesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('roles')->truncate();
        DB::table('role_permissions')->truncate();

        /* 插入 */
        DB::table('roles')->insert([
            'name' => '超级管理员',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);
        DB::table('roles')->insert([
            'name' => '机构管理员',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ]);

        /* 超级管理员拥有权限 */

        $p1ex = [];//超级管理员不拥有权限

        $permissions = DB::table('permissions')->whereNotIn('name', $p1ex)->get();

        foreach ($permissions as $permission) {
            DB::table('role_permissions')->insert([
                'role_id' => 1,
                'permission_id' => $permission->id,
            ]);
        }

        //学校机构管理员默认权限
        $p2 = [
            'device/index',
            'device/view',
            'user/index',
            'user/view',
            'user/add',
            'user/edit',
            'user/enable',
            'user/delete',
            'child/list',
            'child/index',
            'child/view',
            'child/add',
            'child/edit',
            'child/delete',
            'assessment/index',
            'assessment/result',
            'assessment/view',
            'assessment/add',
            'assessment/edit',
            'assessment/delete',
            'training/index',
            'analysis/issuccess',
            'analysis/basecount',
            'analysis/middlecount',
            'analysis/highcount',
            'analysis/children',
        ];
        $permissions2 = DB::table('permissions')->whereIn('name', $p2)->select('id')->get();

        foreach ($permissions2 as $permission) {
            DB::table('role_permissions')->insert([
                'role_id' => 2,
                'permission_id' => $permission->id,
            ]);
        }
    }
}
